
// Copyright (C) 2007 Richard Uhler
// This file is part of Esiply.
//
// Esiply is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License, Version 2,
// as published by the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.

#ifndef _STRING_H_
#define _STRING_H_

// Ecma 8.4 The String Type

//#include <gc_allocator.h>
#include <iostream>
#include <string>

#include "Value.h"

class String;

// Strings are currently represented as stl::strings. This means
// they are not unicode, which is wrong, and will have to be
// changed later.
typedef std::basic_string<char, std::char_traits<char> >gcstring;

class String : public Value
{
public:
    String(const char* cstr, int length);
    String(const char* cstr);
    String(int code);
    String(const String& rhs);

    virtual ValueType getType() const;
    virtual Boolean* toBoolean();
    virtual Object* toObject();
    virtual String* toString();
    virtual Number* toNumber();

    Number* charCodeAt(unsigned int pos) const;

    const char* c_str() const;
    int length() const;

    // Operators
    static Boolean* less_than(String* lhs, String* rhs);
    static String* add(String* lhs, String* rhs);
    static Boolean* equals(String* lhs, String* rhs);

    // For the stl sets, maps, hashtables, etc...
    static size_t hash(String* str);
    static bool equal_to(String* lhs, String* rhs);
    static bool less(String* lhs, String* rhs);

    static Object* prototype;

    friend std::ostream& operator<<(std::ostream& outs, const String& rhs);

private:
    void init_with_cstr(const char* cstr, int length);
    
    gcstring m_string;
};

struct ltstring
{
    bool operator()(String* lhs, String* rhs) const;
};

struct hashstring
{
    size_t operator()(String* str) const;
};

struct equalstring
{
    bool operator()(String* lhs, String* rhs) const;
};

#endif//_STRING_H_

